package cn.doitedu.ml.knn.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 工具： 将手写数字转成1010100101特征向量
 */
public class Image2Vector {

    public static void main(String[] args) throws IOException {

        BufferedWriter bw = new BufferedWriter(new FileWriter("userprofile/data/knndemo/test_vector/vectors.txt"));


        File sampleDir = new File("userprofile/data/knndemo/test/");
        File[] files = sampleDir.listFiles();
        for (File file : files) {
            String label = file.getName().split("_")[1];

            BufferedImage img = ImageIO.read(file);

            StringBuilder sb = new StringBuilder();
            for(int i=0;i<32;i++){
                for(int j=0;j<32;j++){
                    int rgb = img.getRGB(j, i);
                    sb.append(rgb==-1?"0":"1").append(",");

                }
            }
            bw.write(label+","+sb.substring(0,sb.length()-1));
            bw.newLine();
            bw.flush();
        }

        bw.close();

    }

}
